package com.central.apps.controller;

import com.central.app.service.HandlerService;
import com.central.bizcommon.annotation.log.BussinessLog;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import com.central.common.util.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class GetUserInfos {

    @Autowired
    HandlerService handlerService;

    @GetMapping("/getUserInfos")
    @BussinessLog(module = "apps-center", value = "获取当前登录人的部门以及分公司,总公司")
    public Map<String ,Object> GetUserInfos(){
        Map<String, Object> userInfos =new HashMap<>();
        //获取登录用户信息
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        //部门
        String deptcode =loginAppUser.getDeptcode();
        String pcode =deptcode;
		List<Map<String, Object>> deptcodeList = handlerService.findBySql("select fullname from sys_dept where code = '" + deptcode + "'");
		String deptfullname =MapUtils.getString(deptcodeList.get(0), "fullname");
		String pfullname =deptfullname;
        //循环获取一二级,最后一级部门数据,对应总公司,分公司,部门
        boolean flag =true;
        List<String> deptLists =new ArrayList<>();
        deptLists.add(deptcode);
        List<String> deptnameLists =new ArrayList<>();
        deptnameLists.add(deptfullname);
        while(flag){
            List<Map<String, Object>> deptMapList = handlerService.findBySql("select pcode from sys_dept where code ='"+ pcode +"';");
            pcode = (deptMapList.size()>0 && ToolUtil.isNotEmpty(MapUtils.getString(deptMapList.get(0),"pcode")))?MapUtils.getString(deptMapList.get(0),"pcode"):"";
            List<Map<String, Object>> deptMapNameList = handlerService.findBySql("select fullname from sys_dept where code ='"+ pcode +"';");
            pfullname =(deptMapNameList.size()>0 && ToolUtil.isNotEmpty(MapUtils.getString(deptMapNameList.get(0),"fullname")))?MapUtils.getString(deptMapNameList.get(0),"fullname"):"";
            deptLists.add(pcode);
            deptnameLists.add(pfullname);
            if( pcode==null || "".equals(pcode)){
                flag =false;
            }
        }
        String sitecode="",sitefullname="" ,orgcode="" ,orgfullname="";
        if(deptLists!=null && deptLists.size()==2 ){
            orgcode = deptcode;
            orgfullname = deptfullname;
            sitecode = "";
            sitefullname = "";
            deptcode ="";
            deptfullname="";
        }else if(deptLists!=null && deptLists.size()==3 ){
            sitecode =deptcode;
            sitefullname =deptfullname;
            orgcode =deptLists.get(deptLists.size()-2);
            orgfullname =deptnameLists.get(deptnameLists.size()-2);
            deptcode ="";
            deptfullname="";
        }else{
            sitecode =deptLists.get(1);
            sitefullname =deptnameLists.get(1);
            orgcode =deptLists.get(deptLists.size()-2);
            orgfullname =deptnameLists.get(deptnameLists.size()-2);
        }

        // 查询人员台账信息
        List<Map<String, Object>> personList = handlerService.findBySql("select per.id, per.personid, per.name, org.code as \"perorgcode\", org.fullname as \"perorgfullname\", " +
                        " dept.code as \"perdeptcode\", dept.fullname as \"perdeptfullname\", " +
                " site.code as \"persitecode\", site.fullname as \"persitefullname\" from gq_person per left join sys_dept org on org.code = per.orgcode " +
                        " left join sys_dept dept on dept.code = per.deptcode left join sys_dept site on site.code = per.sitecode " +
                " where per.id = (select usr.field10 from sys_user usr where usr.id = " + loginAppUser.getId() + ")");

        if (CollectionUtils.isNotEmpty(personList)) {
            Map<String, Object> personMap = personList.get(0);
            userInfos.put("perkeyid", personMap.get("id"));
            userInfos.put("personid", personMap.get("personid"));
            userInfos.put("personname", personMap.get("name"));
            userInfos.put("perorgcode", personMap.get("perorgcode"));
            userInfos.put("perorgfullname", personMap.get("perorgfullname"));
            userInfos.put("perdeptcode", personMap.get("perdeptcode"));
            userInfos.put("perdeptfullname", personMap.get("perdeptfullname"));
            userInfos.put("persitecode", personMap.get("persitecode"));
            userInfos.put("persitefullname", personMap.get("persitefullname"));
        }

        String deptcodelogin =loginAppUser.getDeptcode();
        userInfos.put("deptcode", deptcodelogin);
        userInfos.put("deptfullname", deptfullname);
        userInfos.put("sitecode", sitecode);
        userInfos.put("sitefullname", sitefullname);
        userInfos.put("orgcode", orgcode);
        userInfos.put("orgfullname", orgfullname);
        userInfos.put("userid", loginAppUser.getId());
        return userInfos;
    }
}
